package org.sky.generator.database.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.Logger;
import org.sky.util.CamelUtils;
import org.sky.util.LogUtils;

/**
 * @author weifx
 * @Title: Column
 * @ProjectName springboot-platform
 * @Description: TODO{数据库列模型}
 * @date 2019/2/25 23:33
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain=true)
public class Column {
    private static Logger logger = LogUtils.getLogger(Column.class);
    /**
     * 表名
     */
    private String tableName;
    /**
     * 字段名
     */
    private String columnName;
    /**
     * 数据类型
     */
    private String dataType;
    /**
     * 字段类型
     */
    private String columnType;
    /**
     * 字段注释
     */
    private String columnComment;
    /**
     * 字段长度
     */
    private int columnSize;
    /**
     * 对应Java属性
     */
    public String getPropertyName(){
        return CamelUtils.toLowerCaseFirstChar(CamelUtils.underlineToCamel(this.columnName));
    }
    /**
     * 对应Java属性类型
     */
    public String getPropertyType(){
        String type = columnType.toLowerCase();
        String propertyType = null;
        if (StringUtils.startsWith(type, "int")) { // int/long
            columnSize = NumberUtils.toInt(StringUtils.substring(type,
                    StringUtils.indexOf(type, "(") + 1,
                    StringUtils.indexOf(type, ")")));
            if (columnSize <= 4) {
                propertyType = "Integer";
            }else {
                propertyType = "Long";
            }
        } else if (StringUtils.startsWith(type, "smallint")) { // long
            propertyType = "Integer";
        }else if (StringUtils.startsWith(type, "bigint")) { // long
            propertyType = "Long";
        } else if (StringUtils.startsWith(type, "double")) { // double
            propertyType = "Double";
        } else if (StringUtils.startsWith(type, "float")) { // float
            propertyType = "Float";
        } else if (StringUtils.startsWith(type, "decimal")) { // float
            propertyType = "java.math.BigDecimal";
        } else if (StringUtils.startsWith(type, "varchar")) { // String
            columnSize = NumberUtils.toInt(StringUtils.substring(type,
                    StringUtils.indexOf(type, "(") + 1,
                    StringUtils.indexOf(type, ")")));
            propertyType = "String";
        } else if (StringUtils.startsWith(type, "char")) { // String
            columnSize = NumberUtils.toInt(StringUtils.substring(type,
                    StringUtils.indexOf(type, "(") + 1,
                    StringUtils.indexOf(type, ")")));
            propertyType = "String";
        } else if (StringUtils.startsWith(type, "text")) { // String
            propertyType = "String";
        } else if (StringUtils.startsWith(type, "date")) { // date
            propertyType = "java.util.Date";
        } else if (StringUtils.startsWith(type, "datetime")) { // date
            propertyType = "java.util.Date";
        } else if (StringUtils.startsWith(type, "timestamp")) { // date
            propertyType = "java.util.Date";
        } else {
            logger.error("类型[" + type + "]解析尚不支持");
        }
        return propertyType;
    }
}
